<?php

namespace app\api\controller;
use app\api\model\Artcile as ArtcileModel;
use app\Request;

class Artcile
{
    public function index(Request $request)
    {
        $key=$request->param('key');
        $page=$request->param('page');
        $limit=$request->param('limit');
        $db = new ArtcileModel();
        if (isset($key)&&!empty($key)) {
            $db = $db->where('title','like','%'.$key.'%');
        }
        $info = $db->page($page)->limit($limit)->select();
        $total = $db->count('id');
        return json(['code'=>200,'msg' =>'成功','data'=>$info,'total'=>$total]);
    }

    public function save(Request $request)
    {
        $data = $request->param();
        $id = $data['id'];
        $db = new ArtcileModel(); // 假设模型名称为ArticleModel
        if (isset($id) && !empty($id)) {
            $res = $db->where('id', $id)->update($data);
        } else {
            $res = $db->save($data);
        }
        if ($res) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 400, 'msg' => '失败']);
        }
    }
    public function delete(Request $request)
    {
        $id=$request->param('id');
        $db = new ArtcileModel();
        $res = $db->where('id',$id)->delete();
        if ($res) {
            return json(['code'=>200,'msg' =>'删除成功']);
        }else{
            return json(['code'=>400,'msg' =>'删除失败']);
        }
    }
}